<?php
namespace Home\Controller;
use Think\Controller;
class TeacherController extends Controller {



    public function addteacher(){
         $admin=M('admin');
$admins=$admin->find();
$this->admin=$admins;
     $this->display();

    }   

Public function doadd(){
        $data=$_POST;
        $file=$_FILES;
        //dump($file['photo']['size']); 
        //dump($file);      
        $TeacherModel = M('teacher'); // 实例化User模型
        $TeacherModel->teacher_name =  I('post.teacher_name'); 
        $TeacherModel->teacher_intro =  I('post.teacher_intro');
        $TeacherModel->teacher_address = I('post.teacher_address');
         //dump($data);
        if ($file['teacher_pic']['size']==0) {//没有上传轮播图片
            //$this->error('请上传背景图片！');                         
        }else{//有图片
            $info=$this->MyUpload();
            $TeacherModel->teacher_pic = $info['teacher_pic']['savename']; // 保存上传的照片根据需要自行组装
        }
        $addresult=$TeacherModel->add(); // 写入用户数据到数据库
        if (false !== $addresult){
            $this->success('上传成功！', '../Teacher/teacherlist');
        }else{ 
            $this->error('数据错误'); 
        }



    }




    public function teacherlist(){
        $admin=M('admin');
$admins=$admin->find();
$this->admin=$admins;
		$teachermodel=M('teacher');
		$teacher=$teachermodel->order('teacher_id asc')->select();
		/*dump($goods);*/
		$this->teacher=$teacher;
		$this->display();
	}






    public function delete($teacher_id){
   /*dump($_GET);
   echo $id;//如果只写上面这3行，注释部分，会再页面显示要删除的数据 
  */ 
    $teacherModel=M('teacher');
        $res=$teacherModel->where("teacher_id=$teacher_id")->find();
        $photoPath = realpath("./Public/upload/teacherpic/".$res['teacher_pic']);
        unlink($photoPath);
        //$res=$teachersmodel->where("id=$id")->delete();
        if ($teacherModel->delete($teacher_id)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }
    }



    Public function doteacher(){
        $data=$_POST;
        $file=$_FILES;
        //dump($file['photo']['size']); 
        //dump($file);      
        $TeacherModel = M('teacher'); // 实例化teacher模型
        $TeacherModel->teacher_name =  I('post.teacher_name'); // 设置用户的创建时间
        $TeacherModel->uploaddate =  date("Y-m-d H:i:s"); // 设置用户的创建时间
        $TeacherModel->teacher_address = I('post.teacher_address');
         //dump($data);
        if ($file['teacher_pic']['size']==0) {//没有上传轮播图片
            //$this->error('请上传背景图片！');                         
        }else{//有图片
            $info=$this->MyUpload();
            $TeacherModel->teacher_pic = $info['teacher_pic']['savename']; // 保存上传的照片根据需要自行组装
        }
        $addresult=$TeacherModel->add(); // 写入用户数据到数据库
        if (false !== $addresult){
            $this->success('上传成功！', '../teacher/teacherlist');
        }else{ 
            $this->error('数据错误'); 
        }
        
    }

     public function MyUpload(){
                //上传头像
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize  = 3145728 ;// 设置附件上传大小
        //$upload->allowExts  = array('mp4','jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');
        $upload->rootPath  =  './';
        $upload->savePath =  'Public/upload/teacherpic/';// 设置附件上传目录

        $upload->autoSub = false;
        $upload->saveName = date("YmdHis").rand(100,999);//定义名字
        // 上传单个文件 
        $info = $upload->upload();                    
            if(!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{// 上传成功 获取上传文件信息
                return $info;
            }
    } 

    function editteacher($teacher_id){//修改用户信息
         $admin=M('admin');
$admins=$admin->find();
$this->admin=$admins;
        $teacher = M('teacher');
        //$data['telephone']=$_SESSION['telephone'];
        $res = $teacher->where("teacher_id = $teacher_id")->find($data);
        $this->list=$res;
        $this->display();
    }
    function doUpdate($teacher_id){
   
        $teachermodel=M('teacher');
        if($_FILES['teacher_pic']['error'] == 0){
            $logoinfo = $this->MyUpload();
            $pname = $logoinfo['file']['savename'];
        } else {
            $pname = "default.jpg";
        }
        $data['teacher_name']=I('post.teacher_name');
        $data['teacher_password']=I('post.teacher_password');
        $data['teacher_pic'] = $pname;
        $data['teacher_phone']=I('post.teacher_phone');
        $data['teacher_club']=I('post.teacher_club');
        $data['teacher_course']=I('post.teacher_course');
        $data['teacher_intro']=I('post.teacher_intro');
        $res=$teachermodel->where("teacher_id = $teacher_id")->save($data);
        if (false!==$res){
            $this->success('信息修改成功','teacherlist');
        } else{
            $this->error('信息修改失败');
        }
    }






    public function xiala(){
  $teacher=M('teacher');//实例化数据库
  $this->club=$club->where("club_name = $club_name")->select();//根据条件筛选查出来的值给lists
  $this->display();
}







}